<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.sectionName" placeholder="版块名称" clearable style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
      :default-sort="{prop: 'sectionId', order: null}"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" fixed width="150px" prop="sectionId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.sectionId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="版块名称" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sectionName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="sectionTop" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.sectionTop===1" size="mini" effect="dark" type="success">已置顶</el-tag>
          <el-tag v-else size="mini" effect="dark" type="info">未置顶</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="最近修改时间" align="center" prop="updateTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="280px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button v-if="row.sectionTop===0" type="success" size="mini" @click="doTop(row.sectionId)">
            置顶
          </el-button>
          <el-button v-if="row.sectionTop===1" type="info" size="mini" @click="cancelTop(row.sectionId)">
            取消置顶
          </el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.sectionId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showForumSectionList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogVisible" width="30%">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-width="100px" style="width: 100%">
        <el-form-item label="版块名称" prop="sectionName">
          <el-input v-model="temp.sectionName" style="width: 90%" />
        </el-form-item>
        <el-form-item label="是否置顶" prop="sectionTop">
          <el-switch v-model="temp.sectionTop" :active-value="1" :inactive-value="0" active-color="#13ce66"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { showForumSectionList, deleteForumSection, saveOrUpdateForumSection } from '@/api/discuss'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'ForumSection',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        sectionName: ''
      },
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      temp: {
        sectionId: undefined,
        sectionName: '',
        sectionTop: 0
      },
      rules: {
        sectionName: [{ required: true, message: '请填写版块名称', trigger: 'change' }],
        sectionTop: [{ required: true, message: '请设置是否置顶', trigger: 'change' }]
      },
      dialogVisible: false
    }
  },
  created() {
    this.showForumSectionList()
  },
  methods: {
    showForumSectionList() {
      this.listLoading = true
      showForumSectionList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showForumSectionList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    resetDialog() {
      this.temp = {
        sectionId: undefined,
        sectionName: '',
        sectionTop: 0
      }
    },
    handleCreate() {
      this.resetDialog()
      this.dialogStatus = 'create'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateForumSection(this.temp).then(() => {
            this.showForumSectionList()
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              message: '新增版块成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateForumSection(this.temp).then(() => {
            this.showForumSectionList()
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              message: '编辑版块成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    doTop(sectionId) {
      saveOrUpdateForumSection({ sectionId: sectionId, sectionTop: 1 }).then(() => {
        this.showForumSectionList()
        this.dialogVisible = false
        this.$notify({
          title: 'Success',
          message: '版块置顶成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    cancelTop(sectionId) {
      saveOrUpdateForumSection({ sectionId: sectionId, sectionTop: 0 }).then(() => {
        this.showForumSectionList()
        this.dialogVisible = false
        this.$notify({
          title: 'Success',
          message: '版块取消置顶成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleDelete(bulletinId) {
      this.$confirm('此操作将删除该版块，该版块下的帖子将无法展示，是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteForumSection(bulletinId).then(() => {
          this.showForumSectionList()
          this.dialogVisible = false
          this.$notify({
            title: 'Success',
            message: '删除版块成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>
